<template>
	<view>
		<view class="myhome">
			<!-- 已登录 -->
			<view class="wx-name" v-if="wxlogin">
				<view>
					<image :src="usering.avatarUrl" mode=""></image>
				</view>
				<view class="wx-text">
					<text>{{usering.nickName}}</text>
				</view>
				<view>

				</view>
			</view>
			<!-- block -->
			<!-- 未登陆 -->
			<view class="wx-button" v-if="!wxlogin">
				<view class="wx-button-view">
					登录汽灵灵外卖，开启吃货旅程
				</view>
				<view>
					<button plain="true" open-type="getUserInfo" @click="getUserInfo">去登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// post请求
	import {
		publicing
	} from '../../api/api.js';
	// 请求地址
	import {
		wxloginurl
	} from '../../api/request.js';
	// 引入类
	let logins = require('@/login/login.js')
	var {
		log
	} = console
	export default {

		data() {
			return {
				wxlogin: false,
				// 用户信息
				usering: {}
			}
		},
		methods: {
			// 获取头像昵称
			getUserInfo(event) {
				//新的登录接口：wx.getUserProfile
				// log(event)
				// 实例化类
				// const logclass = new logins(event)
				wx.getUserProfile({
						desc: '登录'
					})
					.then(res => {
						// this.wxusEr(res.userInfo)
						// console.log(res.userInfo)
						let userInfo = res.userInfo
						userInfo.nickName = '木头'
						let avatarUrl = userInfo.avatarUrl
						let nickName = userInfo.nickName
						// log(nickName)
						this.wxCode(avatarUrl, nickName)
					})
					.catch(err => {
						console.log('拒绝登录或登录失败')
					})
			},
			// 获取code
			wxCode(avatarUrl, nickName) {
				wx.login({
					success: (res) => {
						// log(res)
						let code = res.code
						// log(nickName)
						this.wxLogin(avatarUrl, nickName, code)
					},
					fail: (err) => {
						log(err)
					}
				})
			},
			// 发送请求后端登录
			wxLogin(avatarUrl, nickName, code) {
				// // log(nickName)
				// publicing(wxloginurl,data).then(
				// (res)=>{
				// 	log(res.data)
				// 	if(res.data.msg=="success"){
				// 		// 存入本地
				// 		let data=res.data.datas
				// 		data.nickName="木头"
				// 		uni.setStorageSync('usermen',data)
				// 		this.ifUser()
				// 	}
				// }).catch((err)=>{
				// 	log(err)
				// })

				let data = {
					_id: "61e4d388c2ec5207cfe9ca3e",
					nickName: "木头",
					avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/echWia6oBqia…cTR1JvlSFeeAbefdpDn8mVrcWCTfiaYJYprmic374K4OA/132",
					openid: "oel0F5MpTAP5vcUjE7s-Y1sEs9GE"
				}
				uni.setStorageSync('usermen',data)
				this.ifUser()

			},

			// 取出本地存入的用户信息
			ifUser() {
				let setdata = uni.getStorageSync('usermen')
				log(setdata)
				if (!setdata) {
					log('用户未登录')
					this.wxlogin = false
				} else {
					log('已经登录')
					this.usering = setdata
					this.wxlogin = true
				}
			}
		},
		onShow() {
			log('每次都会执行')
			this.ifUser()
		}
	}
</script>

<style scoped lang="less">
	.myhome {
		background: linear-gradient(to top, #ffe566 10%, #ffd300 100%);
		height: 350upx;
		display: flex;
		align-items: center;

		.wx-name {
			display: flex;
			align-items: center;
			padding: 0 30upx;
			align-content: center;
			height: 200upx;

			image {
				width: 120upx !important;
				height: 120upx !important;
				border-radius: 50%;
				margin-right: 20upx;
				border: 10rpx solid #999999;
			}

			text {
				display: block;
				margin: 10upx 0;
				color: #999999;
			}

			.wx-text text {
				font-size: 35upx;
			}
		}

		/* 登录 */
		.wx-button {
			margin: 0 auto;

			.wx-button-view {
				font-size: 35upx;
				color: #FFFFFF;
				margin-bottom: 25upx;
			}

			button {
				border: none;
				font-size: 30upx;
				background: linear-gradient(to right, #28a6f1 10%, #0e8dd7 80%);
				border-radius: 50upx;
				color: #FFFFFF;
			}


		}
	}
</style>
